package org.joshluisaac.sparklearning;

import static spark.Spark.*;

import java.util.Iterator;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import spark.Request;
import spark.Response;
import spark.Route;

//import spark.*;



public class UploadRestService {

	private static final Logger logger = LoggerFactory
			.getLogger(UploadRestService.class);
	private static boolean isDebugEnabled = true;

	public static void main(String[] args) {

		get(new Route("/hello") {
			@Override
			public Object handle(Request request, Response response) {
				Set<String> requestAttributesSet = request.attributes();
				Iterator<String> requestIterator = requestAttributesSet
						.iterator();

				while (requestIterator.hasNext()) {
					String string = requestIterator.next();
					logger.debug("Printing ...{} object", string);
				}

				if (isDebugEnabled) {
					logger.debug("Host address is {}", request.host());
					logger.debug("IP address is {}", request.ip());
					logger.debug("Port # is: {}s", request.port());
				}
				return "Hello Good World!";
			}
		});

		post(new Route("/hello") {
			@Override
			public Object handle(Request request, Response response) {
				return "Hello World: " + request.body();
			}
		});

		get(new Route("/private") {
			@Override
			public Object handle(Request request, Response response) {
				response.status(401);
				return "Go Away!!!";
			}
		});

		get(new Route("/users/:name") {
			@Override
			public Object handle(Request request, Response response) {
				return "Selected user: " + request.params(":name");
/*				this still works. I used "name" instead of :name
				return "Selected user: " + request.params("name"); */
			}
		});

		get(new Route("/news/:section") {
			@Override
			public Object handle(Request request, Response response) {
				response.type("text/xml");
				return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><news>"
						+ request.params("section") + "</news>";
			}
		});

		get(new Route("/protected") {
			@Override
			public Object handle(Request request, Response response) {
				halt(403, "I don't think so!!!");
				return null;
			}
		});

		get(new Route("/redirect") {
			@Override
			public Object handle(Request request, Response response) {
				response.redirect("/news/world");
				return null;
			}
		});

		get(new Route("/") {
			@Override
			public Object handle(Request request, Response response) {
				return "root";
			}
		});

	}

}
